﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class Tbl_chitietdathang_DAL
    {
        public SqlConnection cn = null;
        public Tbl_chitietdathang_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public Tbl_chitietdathang_DAL(Tbl_chitietdathang_DAL ctdh)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public Tbl_chitietdathang_Collection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("GET_CHITIETDATHANG", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            Tbl_chitietdathang_Collection ctdhCollection = new Tbl_chitietdathang_Collection();

            while (dr.Read())
            {
                Tbl_chitietdathang_Entities ctdh = new Tbl_chitietdathang_Entities();
                ctdh.STT = int.Parse(dr.GetValue(0).ToString());
                ctdh.STT_ctdh = int.Parse(dr.GetValue(1).ToString());
                ctdh.LoaiThe = dr.GetValue(2).ToString();
                ctdh.SoThe = dr.GetValue(3).ToString();

                ctdhCollection.Add(ctdh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ctdhCollection;
        }
        public void insert(Tbl_chitietdathang_Entities ctdh)
        {
            SqlCommand cm = new SqlCommand("INSERT_CHITIETDATHANG", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT_ctdh", ctdh.STT_ctdh);
            cm.Parameters.AddWithValue("@LOAITHE", ctdh.LoaiThe);
            cm.Parameters.AddWithValue("@SOTHE", ctdh.SoThe);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("DELETE_CHITIETDATHANG", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", iD);
            cm.ExecuteNonQuery();
        }
        public void Update(Tbl_chitietdathang_Entities ctdh)
        {
            SqlCommand cm = new SqlCommand("UPDATE_CTDH", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT_ctdh", ctdh.STT_ctdh);
            cm.Parameters.AddWithValue("@LoaiThe", ctdh.LoaiThe);
            cm.Parameters.AddWithValue("@SoThe", ctdh.SoThe);
            cm.Parameters.AddWithValue("@STT", ctdh.STT);
            cm.ExecuteNonQuery();
        }
    }
}
